# MIT License
# Copyright (c) 2018-2025 Jakub Melka and Contributors

cmake_minimum_required(VERSION 3.16)

# ===== 关键：在 project() 之前定义宏 =====
add_compile_definitions(QT_NO_ITERATOR_TRAITS)
add_compile_definitions(QT_NO_EMIT)

# 项目信息
set(PDF4QT_VERSION 1.1.0)
project(Pdf4QtBuild VERSION ${PDF4QT_VERSION} LANGUAGES CXX)

# C++ 标准
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# GNU 标准安装目录
include(GNUInstallDirs)

# Qt 自动工具
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTORCC_OPTIONS "--threshold;0;--compress;9")

# 查找 Qt6 组件
find_package(Qt6 REQUIRED COMPONENTS
    Core
    Gui
    Widgets
    Svg
    Xml
    PrintSupport
    # TextToSpeech
    LinguistTools
)

# 翻译配置
qt_standard_project_setup(I18N_TRANSLATED_LANGUAGES zh_CN)

# 查找第三方依赖
find_package(OpenSSL REQUIRED)
find_package(ZLIB REQUIRED)
find_package(Freetype REQUIRED)
find_package(OpenJPEG CONFIG REQUIRED)
find_package(JPEG REQUIRED)
find_package(PNG REQUIRED)
find_package(blend2d CONFIG REQUIRED)

# 查找 lcms2（颜色管理）
if(VCPKG_TOOLCHAIN)
    find_package(lcms2 REQUIRED)
    set(LCMS2_LIBRARIES lcms2::lcms2 CACHE INTERNAL "LCMS2 libraries")
else()
    find_library(LCMS2_LIBRARIES lcms2 REQUIRED)
endif()

# 检测 Linux GCC 环境并查找 TBB
if(UNIX AND NOT APPLE AND CMAKE_COMPILER_IS_GNUCXX)
    set(LINUX_GCC ON)
    find_package(TBB REQUIRED)
endif()

# 编译选项
if(MSVC)
    add_compile_options(/bigobj /W4 /wd5054 /wd4127 /wd4702)
elseif(MINGW)
    add_compile_options("-Wa,-mbig-obj")
endif()


# ===== 安装目录配置 =====
if(UNIX AND NOT APPLE)
    set(PDF4QT_INSTALL_LIB_DIR ${CMAKE_INSTALL_LIBDIR})
    set(PDF4QT_INSTALL_BIN_DIR ${CMAKE_INSTALL_BINDIR})
else()
    set(PDF4QT_INSTALL_LIB_DIR ${CMAKE_INSTALL_BINDIR})
    set(PDF4QT_INSTALL_BIN_DIR ${CMAKE_INSTALL_BINDIR})
endif()

set(INSTALL_INCLUDEDIR ${CMAKE_INSTALL_INCLUDEDIR}/pdf4qt)

# 输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${PDF4QT_INSTALL_BIN_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${PDF4QT_INSTALL_LIB_DIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${PDF4QT_INSTALL_LIB_DIR})

# 添加子目录
add_subdirectory(Pdf4QtLibCore)
add_subdirectory(Pdf4QtLibWidgets)
add_subdirectory(Pdf4QtLibGui)

# 添加可执行文件用于测试
add_executable(Pdf4QtTestApp
    src/main.cpp
    src/pdfeditortool.cpp
    src/pdfeditortool.h
    src/app.qrc
    src/icon.rc
)

# 链接所需的库
target_link_libraries(Pdf4QtTestApp
    Pdf4QtLibCore
    Pdf4QtLibWidgets
    Pdf4QtLibGui
    Qt6::Core
    Qt6::Gui
    Qt6::Widgets
    Qt6::Svg
    Qt6::Xml
    Qt6::PrintSupport
)

# 设置输出目录
set_target_properties(Pdf4QtTestApp PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)

# 安装配置
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/install CACHE PATH "Installation directory" FORCE)
endif()

# 打印配置信息
message(STATUS "===========================================")
message(STATUS "PDF4Qt Build Configuration")
message(STATUS "===========================================")
message(STATUS "Version: ${PDF4QT_VERSION}")
message(STATUS "TestApp output dir: ${CMAKE_BINARY_DIR}/bin")
message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")
message(STATUS "Library dir: ${PDF4QT_INSTALL_LIB_DIR}")
message(STATUS "Binary dir: ${PDF4QT_INSTALL_BIN_DIR}")
message(STATUS "Include dir: ${INSTALL_INCLUDEDIR}")
message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "===========================================")
